package com.jin.webvideo.service;

import com.jin.webvideo.entity.User;
import com.jin.webvideo.exception.DataException;
import com.jin.webvideo.mapper.UserMapper;
import com.jin.webvideo.util.sys.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ConfigService configService;
    public boolean register(String account, String password, String activekey) throws DataException {
        if(!configService.validActiveKey(activekey)){
            throw new DataException("active key invalid");
        }
        if (userMapper.hasAccount(account)){
            throw new DataException("account has occupation");
        }
        if (null == account || account.isEmpty()){
            account = createAccount();
        }
        String pass = EncryptUtil.md5Hex(password);
        return userMapper.register(account, pass, password, activekey) == 1;
    }
    public User login(String account, String password){
        password = EncryptUtil.md5Hex(password);
        return userMapper.userCanLogin(account, password);
    }
    public String createAccount() throws DataException {
       throw new DataException("account is empty");
    }
}
